package xuecheng.tool.nosql.es.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import xuecheng.tool.json.JsonUtils;
import xuecheng.tool.json.JsonUtilsFactory;
import xuecheng.tool.nosql.es.service.BseElasticSearchService;

/**
 * es 搜索服务基类
 *
 * @author PengXueCheng
 * @version 1.0.0
 **/
@Slf4j
public abstract class BaseElasticSearchServiceImpl<T, ID, M extends ElasticsearchRepository<T, ID>>
    implements BseElasticSearchService<T, ID, M> {

    /**
     * 系统自动装配的客户端对象 注意: 不需要去调用 close 方法
     */
    @Autowired
    protected RestHighLevelClient client;
    @Autowired
    protected M repo;

    @NonNull
    protected JsonUtils jsonUtils = JsonUtilsFactory.obtain();

    @Override
    public <S extends T> S index(S s) {
        return repo.index(s);
    }

    @Override
    public <S extends T> S indexWithoutRefresh(S s) {
        return repo.indexWithoutRefresh(s);
    }

    @Override
    public Iterable<T> search(QueryBuilder queryBuilder) {
        return repo.search(queryBuilder);
    }

    @Override
    public Page<T> search(QueryBuilder queryBuilder, Pageable pageable) {
        return repo.search(queryBuilder, pageable);
    }

    @Override
    public Page<T> search(SearchQuery searchQuery) {
        return repo.search(searchQuery);
    }

    @Override
    public Page<T> searchSimilar(T t, String[] strings, Pageable pageable) {
        return repo.searchSimilar(t, strings, pageable);
    }

    @Override
    public void refresh() {
        repo.refresh();
    }

    @Override
    public Class<T> getEntityClass() {
        return repo.getEntityClass();
    }

    @Override
    public Iterable<T> findAll(Sort sort) {
        return repo.findAll(sort);
    }

    @Override
    public Page<T> findAll(Pageable pageable) {
        return repo.findAll(pageable);
    }

    @Override
    public <S extends T> S save(S s) {
        return repo.save(s);
    }

    @Override
    public <S extends T> Iterable<S> saveAll(Iterable<S> iterable) {
        return repo.saveAll(iterable);
    }

    @Override
    public Optional<T> findById(ID id) {
        return repo.findById(id);
    }

    @Override
    public boolean existsById(ID id) {
        return repo.existsById(id);
    }

    @Override
    public Iterable<T> findAll() {
        return repo.findAll();
    }

    @Override
    public Iterable<T> findAllById(Iterable<ID> iterable) {
        return repo.findAllById(iterable);
    }

    @Override
    public long count() {
        return repo.count();
    }

    @Override
    public void deleteById(ID id) {
        repo.deleteById(id);
    }

    @Override
    public void delete(T t) {
        repo.delete(t);
    }

    @Override
    public void deleteAll(Iterable<? extends T> iterable) {
        repo.deleteAll(iterable);
    }

    @Override
    public void deleteAll() {
        repo.deleteAll();
    }

    /**
     * 解析 hits 中的 _source 数据, 返回对应的实体对象集合
     *
     * @param hits
     *            命中结果
     * @param tClass
     *            实体对象字节码
     * @return 数据实体对象集合
     */
    protected List<T> listSourceData(SearchHit[] hits, Class<T> tClass) {
        return Arrays.stream(hits).map(hit -> {
            float score = hit.getScore();
            String sourceAsString = hit.getSourceAsString();
            log.info("[[**ES查询结果列表**]]: 得分 = {}, source = {}", score, sourceAsString);
            T t = jsonUtils.fromJsonAsObject(sourceAsString, tClass);
            return t;

        }).collect(Collectors.toList());
    }
}
